package com.bbx.taxi.client.Task;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.api.sdk.model.comm.returns.line.To_Cities;
import com.bbx.api.sdk.model.passanger.MyOrder;
import com.bbx.api.sdk.model.passanger.SvconfigBuild;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.config.conn.GetLineNet;
import com.bbx.api.sdk.net.passeger.conn.MyOrderNet_Unfini;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.Menu.MyOrderActivity;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.DB.LineDB;
import com.bbx.taxi.client.DB.MyOrder.MyOrderingDB;

public class MyLineTask {
	private Context context;
	public static MyLineTask instance;
	public LineTask mLineTask;
	private LineDB db_line;// 线路数据库
	public interface OnLineFinishListener {
		public void onLineFinish(boolean isSuccess);
	}

	static OnLineFinishListener mOnLineFinishListener = null;

	public static void setOnLineFinishListener(OnLineFinishListener l) {
		mOnLineFinishListener = l;
	}
	public static MyLineTask getInstance(Context context) {
		instance = new MyLineTask(context);
		return instance;
	}
	public MyLineTask(Context context) {
		this.context = context;
		db_line=new LineDB(context);
		getOnLine();
	}
	/**
	 * 获取线路
	 */
	private void getOnLine() {
		SvconfigBuild mSvconfigBuild = new SvconfigBuild(context);
		mSvconfigBuild.version=0;
		mLineTask = new LineTask(context, mSvconfigBuild);
		mLineTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 线路
	 */
	public class LineTask extends BaseAsyncTask {

		private SvconfigBuild mSvconfigBuild;

		public LineTask(Context context, SvconfigBuild mSvconfigBuild) {
			super(context, false,0);
			this.mSvconfigBuild = mSvconfigBuild;
			setIsReturnString(true);
		}

		@Override
		protected Object doInBackground(Object... params) {
			String json = new JsonBuild().setModel(mSvconfigBuild)
					.getJson();
			return new GetLineNet(context,json);
		}

		@Override
		public void onSuccess(Object object) {
			if(object!=null){
				try {
					JSONObject JO=new JSONObject((String)object);
					db_line.onDelete();
					JSONArray  ja_cities=JO.getJSONArray("cities");
					List<String> list_line=new ArrayList<String>();
					List<String> list_line_cn=new ArrayList<String>();
					//室内
					List<String> list_incity_type=new ArrayList<String>();
					List<String> list_incity_phone=new ArrayList<String>();
					List<String> list_incity_cn=new ArrayList<String>();
					for (int i = 0; i < ja_cities.length(); i++) {
						JSONObject jo = new JSONObject(ja_cities.get(i).toString());
						String name=jo.getString("name");
						String cn_name=jo.getString("cn_name");
						db_line.onWrite(name, cn_name, jo.toString());	
					    /**城际*/
						ArrayList<To_Cities> to_citylist=db_line.readToCityDB(cn_name,false);
						if (to_citylist != null){
			    			for(int j=0;j<to_citylist.size();j++){
			    			    list_line.add(to_citylist.get(j).getLine_id());	
			    			    list_line_cn.add(String.format(context.getString(R.string.line_to_line), cn_name,to_citylist.get(j).getCn_Name()));
			    			    }
			    			}
						/**市内*/
						ArrayList<To_Cities> to_citylist_sn=db_line.readToCityDB(cn_name,true);
						if(to_citylist_sn!=null){
							for(int j=0;j<to_citylist_sn.size();j++){
			    			    //是市内城市
			    			    if(to_citylist_sn.get(j).getIs_incity()!=null){
			    			    if(to_citylist_sn.get(j).getIs_incity().equals(Value.CITY_INCITY)&&to_citylist_sn.get(j).getCn_Name().equals(cn_name)){
			    			    	list_incity_type.add(to_citylist_sn.get(j).getIncity_type());	
			    			    	list_incity_phone.add(to_citylist_sn.get(j).getIncity_phone());	
			    			    	list_incity_cn.add(to_citylist_sn.get(j).getCn_Name());
			    			    }
			    			    }
			    			}
						}
					}
					//存储线路
					SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_line_num,list_line.toString().trim());
					SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_line_cn,list_line_cn.toString().trim());
					//市内城市
					SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_incity_phone,list_incity_phone.toString().trim());
					SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_incity_type,list_incity_type.toString().trim());
					SharedPreUtil.putStringValue(context,SharedPreEncryptUtil.list_incity_cn,list_incity_cn.toString().trim());
				} catch (JSONException e) {
				}
				if(mOnLineFinishListener!=null){
					mOnLineFinishListener.onLineFinish(true);
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			if(mOnLineFinishListener!=null){
				mOnLineFinishListener.onLineFinish(false);
			}
		}
	}
}
